<template>
  <div class="homepage-hero-module">
    <div class="video-container">
      <div :style="fixStyle" class="filter"></div>
      <video :style="fixStyle" autoplay loop muted class="fillWidth" v-on:canplay="canplay">
        <!--        <source src="https://cdn.jsdelivr.net/gh/xdlumia/files/video-play/IronMan.mp4" type="video/mp4"/>-->
        <source :src="src" type="video/mp4"/>
        浏览器不支持 video 标签，建议升级浏览器。
      </video>
      <div class="poster hidden" v-if="!vedioCanPlay">
        <img :style="fixStyle" :src="defImageSrc" alt="">
      </div>
    </div>
  </div>
</template>
<script>

export default {

  name: 'customVideoBG',

  props:{
    src:{
      type:String,
      default:require('@/assets/video/myWorld.mp4')
    },
    defImageSrc:{
      type:String,
      default: require('@/assets/images/myWorld.jpeg')
    },
    filterSize:{
      type:Number,
      default:6
    }
  },

  data() {

    return {

      vedioCanPlay: false,

      fixStyle: ''

    }

  },

  methods: {

    canplay() {

      this.vedioCanPlay = true

    }

  },

  mounted: function () {

    window.onresize = () => {

      const windowWidth = document.body.clientWidth

      const windowHeight = document.body.clientHeight

      const windowAspectRatio = windowHeight / windowWidth

      let videoWidth

      let videoHeight

      if (windowAspectRatio < 0.5625) {

        videoWidth = windowWidth

        videoHeight = videoWidth * 0.5625

        this.fixStyle = {

          height: windowWidth * 0.5625 + 'px',

          width: windowWidth + 'px',

          'margin-bottom': (windowHeight - videoHeight) / 2 + 'px',

          'margin-left': 'initial',

          'backdrop-filter': `blur(${this.filterSize}px)`

        }

      } else {

        videoHeight = windowHeight

        videoWidth = videoHeight / 0.5625

        this.fixStyle = {

          height: windowHeight + 'px',

          width: windowHeight / 0.5625 + 'px',

          'margin-left': (windowWidth - videoWidth) / 2 + 'px',

          'margin-bottom': 'initial',

          'backdrop-filter': `blur(${this.filterSize}px)`

        }

      }

    }

    window.onresize()

  }

}

</script>
<style scoped lang="scss">
.homepage-hero-module {
  .video-container {

    position: relative;

    height: 100vh;

    overflow: hidden;

  }

  .video-container .poster img,
  .video-container video {

    z-index: 0;

    position: absolute;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;

  }

  .video-container .filter {

    z-index: 1;

    position: absolute;

    background: rgba(0, 0, 0, 0.4);

  }
}

</style>
